package com.cloud.code.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import java.util.concurrent.*;


/**
 * 实现了一个服务端通过多线程处理 多个客户端的请求的功能
 * 注意：  服务端 是先读取，在写入
 *        客户端 是先写入，在读取
 */
public class Server {

    private static Vector vector = new Vector();


    public static void main(String[] args) {


        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        //端口1024-65535
        try {
            ServerSocket serverSocket = new ServerSocket(6666);
            System.out.println("服务端已经启动 ，等待客户端链接");
            while (true) {
                Socket accept = serverSocket.accept();

                threadPoolExecutor.execute(new UserSocketServiceThread(vector,accept));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
